package com.zjmzxfzhl.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zjmzxfzhl.modules.app.entity.*;
import com.zjmzxfzhl.modules.app.form.AppProdVo;
import com.zjmzxfzhl.modules.app.form.DocTotalVo;
import com.zjmzxfzhl.modules.app.service.*;
import com.zjmzxfzhl.modules.sys.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zjmzxfzhl.common.core.base.BaseServiceImpl;
import com.zjmzxfzhl.modules.app.mapper.AppProdMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Service
 * 
 * @author 张森荣
 */
@Service
public class AppProdServiceImpl extends BaseServiceImpl<AppProdMapper, AppProd>
        implements AppProdService {

    @Autowired
    private AppProdTagService appProdTagService;
    @Autowired
    private AppProdUserService appProdUserService;
    @Autowired
    private AppProdVerService appProdVerService;
    @Autowired
    private AppDocService appDocService;

    @Override
    public IPage<AppProdVo> list(IPage<AppProdVo> page, AppProdVo appProdVo) {
        return page.setRecords(baseMapper.list(page, appProdVo));
    }

    /**
     * 新增项目，自动计算机构级别、机构级次码、是否叶子
     *
     * @param appProd
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAppProd(AppProd appProd) {
        //保存产品成员数组，标签数组
        List<SysUser> userList=null;
        List<AppTag> tags=null;
        if(appProd.getUserList()!=null){
            userList=appProd.getUserList();
        }
        if(appProd.getTags()!=null){
            tags=appProd.getTags();
        }
        //添加项目
        appProd =setAppProd(appProd);
        Boolean result = this.save(appProd);
        if(result==false){
            return result;
        }
        appProd=this.getById(appProd.getProdId());

        //添加项目成员，项目标签
        if(userList!=null){
            for (SysUser user:userList) {
                AppProdUser appProdUser=new AppProdUser();
                appProdUser.setProdId(appProd.getProdId());
                appProdUser.setUserId(user.getUserId());
                appProdUserService.saveAppProdUser(appProdUser);
            }
        }
        if(tags!=null){
            for (AppTag tag:tags) {
                AppProdTag appProdTag=new AppProdTag();
                appProdTag.setProdId(appProd.getProdId());
                appProdTag.setTagId(tag.getTagId());
                appProdTagService.saveAppProdTag(appProdTag);
            }
        }
        return result;
    }

    private AppProd setAppProd(AppProd appProd){

        return appProd;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AppProdVo queryById(String id) {
        AppProdVo appProdVo=new AppProdVo();
        appProdVo.setProdId(id);
        List<AppProdVo> appProdVoList= baseMapper.list(null, appProdVo);
        if(appProdVoList.size()>0){
            return appProdVoList.get(0);
        }
        return null;
    }

    /**
     * 新增机构，自动计算机构级别、机构级次码、是否叶子
     *
     * @param appProd
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAppProd(AppProd appProd) {
        //保存修改标签数组
        List<SysUser> userList=null;
        List<AppTag> tags=null;
        if(appProd.getUserList()!=null){
            userList=appProd.getUserList();
        }
        if(appProd.getTags()!=null){
            tags=appProd.getTags();
        }
        appProd.setUpdateTime(new Date());
        Boolean result = this.updateById(appProd);
        if(result==false){
            return false;
        }

        //删除标签
        //添加标签
        if(userList!=null){
            AppProdUser appProdUser=new AppProdUser();
            appProdUser.setProdId(appProd.getProdId());
            appProdUserService.relationDel(appProdUser);
            for (SysUser user:userList) {
                appProdUser.setUserId(user.getUserId());
                appProdUserService.saveAppProdUser(appProdUser);
            }
        }
        if(tags!=null){
            AppProdTag appProdTag=new AppProdTag();
            appProdTag.setProdId(appProd.getProdId());
            appProdTagService.relationDel(appProdTag);
            for (AppTag tag:tags) {
                appProdTag.setTagId(tag.getTagId());
                appProdTagService.saveAppProdTag(appProdTag);
            }
        }
        return result;
    }

    /**
     * 导入期
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean introduction(String prodId) {
        AppProd appProd=this.getById(prodId);
        if(appProd==null){
            return false;
        }
        appProd.setStatus("00");
        appProd.setUpdateTime(new Date());
        Boolean result = this.updateById(appProd);

        return result;
    }

    /**
     * 成长期
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean anagen(String prodId) {
        AppProd appProd=this.getById(prodId);
        if(appProd==null){
            return false;
        }
        appProd.setStatus("01");
        appProd.setUpdateTime(new Date());
        Boolean result = this.updateById(appProd);

        return result;
    }

    /**
     * 成熟期
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean maturation(String prodId) {
        AppProd appProd=this.getById(prodId);
        if(appProd==null){
            return false;
        }
        appProd.setStatus("02");
        appProd.setUpdateTime(new Date());
        Boolean result = this.updateById(appProd);

        return result;
    }

    /**
     * 哀退期
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean catagen(String prodId) {
        AppProd appProd=this.getById(prodId);
        if(appProd==null){
            return false;
        }
        appProd.setStatus("03");
        appProd.setUpdateTime(new Date());
        Boolean result = this.updateById(appProd);

        return result;
    }

    /**
     * 已关闭
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean close(String prodId) {
        AppProd appProd=this.getById(prodId);
        if(appProd==null){
            return false;
        }
        appProd.setStatus("04");
        appProd.setUpdateTime(new Date());
        Boolean result = this.updateById(appProd);

        return result;
    }

    /**
     * 统计产品概括
     *
     * @return
     */
    @Override
    public Map<String, Object> summarize(String prodId) {
        Map<String, Object> result=new HashMap<>();
        //发布版本数量
        Integer verNum = appProdVerService.count(new LambdaQueryWrapper<AppProdVer>().eq(AppProdVer::getProdId, prodId));
        result.put("verNum",verNum);

        //产品物料数量
        AppDoc appDoc=new AppDoc();
        appDoc.setDocType("02");
        appDoc.setDocTypeId(prodId);
        appDoc.setDeleted("0");
        List<DocTotalVo> docTotalVos=appDocService.docTotal(appDoc);
        if(docTotalVos==null){
            docTotalVos=new ArrayList<>();
        }
        result.put("docTotalVos",docTotalVos);

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delByIds(String ids) {
        Boolean result = true;
        String[] idsArr = ids.split(",");

        for (String s:idsArr) {
            //删除产品文档
            Map<String, Object> columnMap=new HashMap<>();
            columnMap.put("DOC_TYPE_ID",s);
            columnMap.put("DOC_TYPE","02");
            appDocService.removeByMap(columnMap);
            //删除产品
            result= this.removeById(s);
        }
        return result;
    }
}
